package io.hxci.modules.stu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hxci.common.utils.Constant;
import io.hxci.common.utils.PageUtils;
import io.hxci.common.utils.Query;
import io.hxci.common.utils.R;
import io.hxci.modules.edu.domain.EduStudent;
import io.hxci.modules.edu.service.EduStudentService;
import io.hxci.modules.stu.domain.EvalApply;
import io.hxci.modules.stu.domain.EvalRecord;
import io.hxci.modules.stu.domain.EvalType;
import io.hxci.modules.stu.service.EvalApplyService;
import io.hxci.modules.stu.service.EvalRecordService;
import io.hxci.modules.stu.service.EvalTypeService;
import io.hxci.modules.sys.controller.AbstractController;
import io.hxci.modules.sys.entity.SysUserEntity;
import io.hxci.modules.sys.service.SysUserService;
import io.hxci.modules.zhangcong.domain.Zcuser;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("eval")
public class EvalController  extends AbstractController {
    @Autowired
    EvalRecordService service;
    @Autowired
    EvalTypeService eservice;
    @Autowired
    EvalApplyService applyService;
    @Autowired
    EduStudentService eduStudentService;
    @Autowired
    SysUserService userService;

    //个人信息中得失分记录
    @RequestMapping("record")
    public List<EvalRecord> record(Integer pid){
        Long sid = getUserId();
        return  service.record(sid,pid);
    }

    //获取所有一级得失分项目
    @RequestMapping("type")
    public List<EvalType> type(){
        return   eservice.list(new QueryWrapper<EvalType>().eq("parent_id",0));
    }

    //获取所有一级得分项目
    @RequestMapping("typea")
    public List<EvalType> typea(){
        return   eservice.list(new QueryWrapper<EvalType>().eq("parent_id",0).eq("del_flag",1));
    }
    //获取所有二级得分项目 springmvc 接收值
    @RequestMapping("typeb")
    public List<EvalType> typeb(Long pid){
        return   eservice.list(new QueryWrapper<EvalType>().eq("parent_id",pid).eq("del_flag",1));
    }


    //添加申请的接口
    @RequestMapping("applyAdd")
    public R applyAdd(@RequestBody EvalApply apply){
        Long sid = getUserId();//当前登录的学生的id

        if(apply.getId() == null || "".equals(apply.getId())){
            //根据学生id查询导员id
            EduStudent student =eduStudentService.info(sid);
            apply.setTime(createTime());
            apply.setSid(sid);
            apply.setTid(student.getGid());
            apply.setSign(createSign());
        }

        applyService.saveOrUpdate(apply);
        return R.ok();
    }


    //查看申请的接口
    @RequestMapping("applyQuery")
    public R applyQuery(Map<String, Object> params){

        Long sid = getUserId();//当前登录的学生的id
        List <EvalApply> list =applyService.list(
                new QueryWrapper<EvalApply>().eq("sid",sid).eq("status",0) .orderByDesc("createtime"));

        for (EvalApply apply: list) {
           SysUserEntity user = userService.getById(apply.getTid());
            EvalType evalType =  eservice.getById(apply.getBtype());
            apply.setGname(user.getUsername());
            apply.setTname(evalType.getName());
        }

        return R.ok().put("list",list);
    }

    //修改加分记录
    @RequestMapping("edit")
    public R edit(Integer id){
        EvalApply apply =applyService.getById(id);
        EvalType type =  eservice.getById(apply.getBtype());
        apply.setAtype(type.getParentId());
        List<EvalType> list= eservice.list(new QueryWrapper<EvalType>().eq("parent_id",type.getParentId()));
        return   R.ok().put("list",list).put("apply",apply);
    }

    @RequestMapping("del")
    public R del(Integer id){
         applyService.removeById(id);
         return   R.ok();
    }

    @RequestMapping("fileUpload")
    public R  fileUpload(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file"); // 通过参数名获取指定文件
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String originalFilename = new Date().getTime()+type;
        Long sid = getUserId();
        EduStudent student =eduStudentService.info(sid);

        File uploadDir = new File(Constant.UPLOADURL +student.getSno());
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        File newFile=new File(uploadDir+"/"+originalFilename);
        file.transferTo(newFile);
        System.out.println("上传路径："+basePath+"/statics/image/"+student.getSno()+"/"+originalFilename);
        return R.ok().put("img", "/statics/image/"+student.getSno()+"/"+originalFilename);
    }





    public String createSign(){
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHmHmss"); // 获取当前时间

        Random random = new Random(); // 定义随机数
        int ran = random.nextInt(10000); // 随机数长度位数

        Date date = new Date();
        String dateran = dmDate.format(date);
        String randomMath = dateran+ran;
        return randomMath;

    }

    public String createTime(){
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMdd"); // 获取当前时间
        Date date = new Date();
        String dateran = dmDate.format(date);
       return dateran;

    }
}
